package com.controller;

import com.domain.Person;
import com.mysql.cj.xdevapi.JsonArray;
import com.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/gst")
public class Personcontroller {
    @Autowired
    private PersonService personservice;











    @RequestMapping(value ="/addperson",produces = "application/json;charset=utf-8")
    @ResponseBody
    public JsonArray json1(HttpServletRequest request,  ServletResponse response)throws IOException
    {
        JsonArray json=new JsonArray();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String querypassword=request.getParameter("querypassword");
        List<Person> findallperson=personservice.findallperson();
        if(name.equals("")||password.equals("")||querypassword.equals(""))
        {
            json.add(new Message("增加失败，输入信息不能为空"));
            return json;

        }
        if(!password.equals(querypassword))
        {
            json.add(new Message("增加失败，两次密码不相等"));
            return json;

        }
        for(Person one:findallperson)
        {
            if(one.name.equals(name))
            {
                json.add(new Message("新增用户失败，该用户已经存在"));
                return json;
            }
        }
        json.add(new Message("增加用户信息成功"));
        Person person=new Person();
        if(findallperson.size()<=0)
        { person.setId(1);}
        else
        {
            person.setId(findallperson.get(findallperson.size()-1).getId()+1);
        }
        person.setName(name);
        person.setPassword(password);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        person.setCreate_time(df.format(new Date()));
        person.setUpdate_time(df.format(new Date()));
        personservice.addperson(person);
        return json;
    }



    @RequestMapping(value ="/chaxu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public JsonArray json2(HttpServletRequest request,  ServletResponse response)throws IOException {
System.out.println("查询1");

         String id= request.getParameter("id");
         String name=request.getParameter("name");

        JsonArray json1=new JsonArray();

        List<Person> searchall=personservice.searchall(id,name);

        for(Person one1:searchall)
        {   json1.add(new Message("查询用户信息成功"));
            json1.add(one1);
            return json1;

        }

        json1.add(new Message("查询不存在该用户信息"));
        return json1;

    }
    @RequestMapping(value ="/editperson",produces = "application/json;charset=utf-8")
    @ResponseBody
    public JsonArray json3(HttpServletRequest request,  ServletResponse response)throws IOException {
     String id=request.getParameter("uid");
     String name=request.getParameter("uname");
     String password=request.getParameter("upassword");
     System.out.println("gsa"+id+name+password);
     JsonArray json1=new JsonArray();


     if(name.equals("")||password.equals(""))
     {


         json1.add(new Message("修改失败！修改信息不能为空"));

     }
     personservice.editperson(id,name,password);
     json1.add(new Message("修改用户信息成功"));
        return json1;

    }

    @RequestMapping(value ="/delperson",produces = "application/json;charset=utf-8")
    @ResponseBody
    public JsonArray json4(HttpServletRequest request,  ServletResponse response)throws IOException {
        String id=request.getParameter("delid");
        System.out.println("删除的id为"+request.getParameter("delid"));
        JsonArray json1=new JsonArray();
        personservice.delperson(id);
        json1.add(new Message("删除用户信息成功"));
        return  json1;
    }





    @RequestMapping("showall")
    public ModelAndView findalluser(HttpServletRequest request, ServletResponse response)
    {
        List<Person> findallperson=personservice.findallperson();
        System.out.println("长度为:");
        System.out.println(findallperson.size());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("persondata", findallperson);

        for(Person one:findallperson)
        {
            modelAndView.setViewName("admin/showall");

            System.out.println(one.name);
            return modelAndView;
        }
        modelAndView.setViewName("admin/showall");
        return modelAndView;
    }




    @RequestMapping("/findallperson")
    public ModelAndView findallperson(HttpServletRequest request,  ServletResponse response) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        String name=(String) request.getParameter("name");
        String password=(String) request.getParameter("pwd");
System.out.println("账号："+name);
System.out.println("密码:"+password);
        List<Person> search1=personservice.findallperson();
        response.setContentType("text/html;charset=utf-8");
        request.getSession().setAttribute("USER_SESSION","exist");

        try {
            if (name.equals("") || password.equals("")) {
                JOptionPane.showMessageDialog(null, "输入数值不能为空");
                modelAndView.setViewName("index");
                return modelAndView;
            }
            for (Person one : search1) {
                if (name.equals(one.name) && password.equals(one.password)) {
                    JOptionPane.showMessageDialog(null, one.name+one.password+"登录成功");
                    modelAndView.setViewName("login");
                    return modelAndView;
                }
            }
            JOptionPane.showMessageDialog(null, "不存在该账号或密码");
            modelAndView.setViewName("index");
            return modelAndView;
        }catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "不可访问 没有登录");
            modelAndView.setViewName("index");
            return modelAndView;
        }

    }





}
